﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace PInvokeTest
{
     class Program
    {
        static void Main(string[] args)
        {
            TestSection();
            Console.Read();
        }

        //验证文字，以及指针获取数据
        static void TestSection()
        {
            Joint jt = new Joint();
            RectangleSection rec = new RectangleSection() {name="hello section", Width = 200, Height = 400 };
            jt.section = rec;
            Console.WriteLine("C#Size"+Marshal.SizeOf(typeof(RectangleSection)));
            
        }
        //测试数组
        static void TestJointArr()
        {
            Joint jt = new Joint();
            NSJointLoad load1 = new NSJointLoad();
            load1.id = 10;
            load1.fx = 200;
            load1.fy = 300;
            NSJointLoad load2 = new NSJointLoad();
            load2.id = 30;
            load2.fx = 800;
            load2.fy= 800;
            NSJointLoad[] loads = new NSJointLoad[] { load1, load2 };
            jt.loadArr = loads;

            string a = "first";
            string b = "second";

            string[] strarr = new string[] { a, b };
            jt.strArr = strarr;
        }
        //测试pinvokeload
        static void TestJoint()
        {
            Joint jt = new Joint();
            NSJointLoad load = new NSJointLoad();
            load.id = 10;
            load.fx = 200
;           jt.jointLoad = load;
            NSJointLoad outLoad = jt.jointLoad;
            Console.WriteLine(outLoad.id);
            Console.WriteLine(outLoad.fx);
            Console.WriteLine("C#size:"+Marshal.SizeOf(typeof(NSJointLoad)));
            Console.Read();
        }
        static void Test06()
        {
            Student student = new Student() { age = 18 };
            var ptr = Marshal.AllocHGlobal(Marshal.SizeOf( student));
            Marshal.StructureToPtr(student, ptr, false);
            TeacherP teacher = new TeacherP() { age = 30, student = ptr };
            TeacherP.Call(teacher);

        }
        static void Test05()
        {
            Student student = new Student() { age = 18 };
            PureTeacher teacher = new PureTeacher() { age = 30,student=student};
            PureTeacher.Call(teacher);
      
        }
        static void Test04()
        {
            Person person = new Person();
            UnsafenativeMethod.SetJob(person, Job.teacher);
            Console.WriteLine(person.job);
            Console.ReadLine();
        }
        static void Test03()
        {
            Person person = new Person();
            ////person.Eat();
            person.age = 10;
            Console.WriteLine(person.age);

            
            GCHandle m_gch = GCHandle.Alloc(person, GCHandleType.Pinned);
            IntPtr personAdress= GCHandle.ToIntPtr(m_gch);
            Console.WriteLine("Person地址:" + personAdress);
            GCHandle m_gch_age = GCHandle.Alloc(person.age, GCHandleType.Pinned);
            IntPtr personAgeAdress = GCHandle.ToIntPtr(m_gch_age);
            Console.WriteLine("Personage地址:"+personAdress);
            UnsafenativeMethod.SetPersonAge(person, 30);

            Console.WriteLine(person.age);
            //m_gch.Free();
            //DoTest();
            Console.Read();
        }
        static void Test02()
        {
            double originalAge = 20;
            GCHandle m_gch = GCHandle.Alloc(originalAge, GCHandleType.Pinned);
            UnsafenativeMethod.SetPersonAge2(ref originalAge, 30);
            Console.WriteLine(originalAge);
            m_gch.Free();
            Console.Read();
        }
        static void DoTest()
        {
            UInt32 m_uiStatus = 0;
            GCHandle m_gch = GCHandle.Alloc(m_uiStatus, GCHandleType.Pinned);

            UnsafenativeMethod.someMethod(ref m_uiStatus);

            m_gch.Free();
        }
    }
}
